package com.jiugang.cardump.controller;


import com.jiugang.cardump.bean.InformationBean;
import com.jiugang.cardump.constant.StatusCodeConstant;
import com.jiugang.cardump.entity.Equipment;
import com.jiugang.cardump.exception.SystemExceptionV110;
import com.jiugang.cardump.result.ResultInfoV110;
import com.jiugang.cardump.service.EquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-08-19
 */
@RestController
@RequestMapping("/equipment")
@Slf4j
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    /**
     * @param equipment
     * @return ResultInfoV110
     * @Description 添加一台设备
     */
    @PostMapping
    public ResultInfoV110 addEquipment(Equipment equipment) {
        log.debug("EquipmentController的addEquipment方法进入,参数equipment为" + equipment);
        //参数校验
        if (equipment == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "添加设备"));
        }
        if (equipment.getId() == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "添加设备编号"));
        }
        if (equipment.getEquipmentName() == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "添加设备名称"));
        }
        return equipmentService.insertEquipment(equipment);
    }


    /**
     * @param id 设备编号
     * @param statu 设备状态
     * @return ResultInfoV110
     * @Description 更新设备状态
     */
    @PutMapping("/status")
    public ResultInfoV110 updateEquipmentStatu(String id,Integer statu){
        log.debug("EquipmentController的updateEquipmentStatu方法进入,id" + id+",参数statu为"+statu);
        //参数校验
        if(id == null || statu == null){
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002,"更新设备状态"));
        }
        return equipmentService.updateEquipmentStatu(id, statu);
    }

    /**
     * @param id 设备编号
     * @return ResultInfoV110
     * @Description 根据设备id查询设备信息
     */
    @GetMapping("/info/{id}")
    public ResultInfoV110 queryEquipmentById(@PathVariable("id") String id){
        log.debug("EquipmentController的queryEquipmentById方法进入,id" + id);
        //参数校验
        if(id == null){
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002,"查询设备信息"));
        }
        return  equipmentService.selectEquipmentById(id);
    }

    /**
     * @param equipment
     * @return ResultInfoV110
     * @Description 修改设备信息
     */
    @PutMapping
    public ResultInfoV110 updateEquipment(Equipment  equipment){
        log.debug("EquipmentController的updateEquipment方法进入,参数equipment为" + equipment);
        //参数校验
        if(equipment == null){
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002,"修改设备信息"));
        }
        if(equipment.getId() == null){
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002,"修改设备设备编号"));
        }
        return equipmentService.updateEquipment(equipment);

    }

    /**
     * @param equipment
     * @return ResultInfoV110
     * @Description  根据设备编号和设备名称检索所有的设备接口
     */
    @GetMapping("/list")
    public ResultInfoV110 queryAllEquipmentByCondition(Equipment equipment){
        log.debug("EquipmentController的updateEquipment方法进入,参数equipment为" + equipment);
        if(equipment == null){
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002,"查询设备列表信息"));
        }
        return equipmentService.selectAllEquipmentByCondition(equipment);
    }


    /**
     * @param equipmentId
     * @return ResultInfoV110
     * @Description  根据设备编号删除设备
     *
     */
    @DeleteMapping("{id}")
    public ResultInfoV110 removeEquipmentById(@PathVariable("id") String equipmentId){
         log.debug("EquipmentController的removeEquipmentById方法进入,参数equipmentId为" + equipmentId);
         if(equipmentId == null){
             throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002,"删除设备"));
         }
         return equipmentService.deleteEquipmentById(equipmentId);
     }
}
